package com.syezon.wifikey.speed;

import android.arch.lifecycle.MutableLiveData;


import com.syezon.android.base.utils.DeviceUtils;
import com.syezon.wifikey.api.HttpClient;
import com.syezon.wifikey.utils.Utils;

import org.json.JSONArray;
import org.json.JSONObject;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class DeviceInfoService {

    private static volatile DeviceInfoService instance;

    private MutableLiveData<DeviceInfo> deviceInfoMutableLiveData = new MutableLiveData<>();

    private DeviceInfoService() {

    }

    public MutableLiveData<DeviceInfo> getDeviceInfoMutableLiveData() {
        return deviceInfoMutableLiveData;
    }

    public static DeviceInfoService getInstance() {
        if (instance == null) {
            synchronized (DeviceInfoService.class) {
                if (instance == null) {
                    instance = new DeviceInfoService();
                }
            }
        }
        return instance;
    }

    public void getDeviceInfo() {
       Observable.create(new ObservableOnSubscribe<DeviceInfo>() {
            @Override
            public void subscribe(ObservableEmitter<DeviceInfo> e) throws Exception {
                DeviceInfo localDeiveInfo = getLocalDeiveInfo();
                try {
                    String result = HttpClient.getResult("http://ip.taobao.com/service/getIpInfo.php?ip=myip", "");
                    JSONObject netInfoJson = new JSONObject(result);
                    int code = netInfoJson.optInt("code");
                    if (code == 0) {
                        JSONObject dataInfoJson = netInfoJson.optJSONObject("data");
                        String outIp = dataInfoJson.optString("ip", "未知");
                        String isp = dataInfoJson.optString("isp", "未知");
                        localDeiveInfo.setOuterIp(outIp);
                        localDeiveInfo.setIsp(isp);
                        e.onNext(localDeiveInfo);
                    }else {
                        String backResult = HttpClient.getResult("http://api.ip138.com/query/?token=53237a5c33dc8f71d60a229331bc9760", "");
                        JSONObject jo = new JSONObject(backResult);
                        localDeiveInfo.setOuterIp(jo.optString("ip", "未知"));
                        JSONArray dataArr = jo.optJSONArray("data");
                        if (dataArr != null && dataArr.length() >= 4) {
                            localDeiveInfo.setIsp(dataArr.optString(3));
                        }
                    }
                } catch (Exception ex) {

                }
                e.onNext(localDeiveInfo);
            }
        }).subscribeOn(Schedulers.io())
                .subscribe(new Observer<DeviceInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(DeviceInfo deviceInfo) {
                        deviceInfoMutableLiveData.postValue(deviceInfo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        DeviceInfo deviceInfo = getLocalDeiveInfo();
                        deviceInfoMutableLiveData.postValue(deviceInfo);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private DeviceInfo getLocalDeiveInfo() {
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.setModel(DeviceUtils.getModel());
        deviceInfo.setIsp("unknown");
        deviceInfo.setNetwordType(NetInfoUtils.getNetType(Utils.getApp()));
        deviceInfo.setInnerIp(NetInfoUtils.getLocaleIp(Utils.getApp()));
        deviceInfo.setOuterIp("unknown");
        return deviceInfo;
    }

}
